﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SanctusSoftware.Music.Chords;
//using SanctusSoftware.Spelling;

namespace SanctusSoftware.Music.Chords
{
  public static class SentenceTransposer
  {

    
    //public static int GetIsChordSentencePercent(string sentence)
    //{

    //  decimal chordCharacterCount = 0;
    //  decimal chordWordCount = 0;

    //  foreach (Match match in Regex.Matches(sentence, ChordTransposer.ChordPattern))
    //  {

    //    if (Regex.IsMatch(match.Value, "^[BCDFGbcdfg]$"))
    //    {
    //      // If it's a single letter and not a vowel, it's a chord    
    //      // Dumb dictionary considers "B" a word
    //      chordCharacterCount += Convert.ToDecimal(match.Value.Length);
    //      chordWordCount++;
    //    }
    //    else if (UnicodeNormalizationHelper.ContainsDiacritics(match.Value))
    //    {
    //      // If it contains Diacritics, it's probably a WORD
    //      // Do nothing
    //    }
    //    else if (Regex.IsMatch(match.Value, "[0-9]"))
    //    {
    //      // If it contains numbers, it's probably a CHORD
    //      chordCharacterCount += Convert.ToDecimal(match.Value.Length);
    //      chordWordCount++;
    //    }
    //    else if (Regex.IsMatch(match.Value, "[" + ChordTransposer.SymbolsUsedInChords + "]"))
    //    {
    //      // If it contains symbols used in chords, it's probably a CHORD
    //      chordCharacterCount += Convert.ToDecimal(match.Value.Length);
    //      chordWordCount++;
    //    }
    //    else if (Regex.IsMatch(UnicodeNormalizationHelper.RemoveDiacritics(match.Value), "[^A-Za-z]"))
    //    {
    //      // If it has non-word characters, it's probably a chord
    //      chordCharacterCount += Convert.ToDecimal(match.Value.Length);
    //      chordWordCount++;
    //    }
    //    else if (ChordTransposer.ValidChordsValidWords.Contains(match.Value))
    //    {
    //      // Might be a chord, Might Not 
    //      // Give it a 51% chance of being a chord
    //      chordCharacterCount += (Convert.ToDecimal(match.Value.Length) * 0.51m);
    //      chordWordCount += 0.51m;
    //    }
    //    else if (ChordTransposer.ValidChordsInvalidWords.Contains(match.Value))
    //    {
    //      // Some words are valid in the dictionary but realistically, they're NOT song lyrics but instead chords
    //      chordCharacterCount += Convert.ToDecimal(match.Value.Length);
    //      chordWordCount++;
    //    }
    //    else
    //    {

    //      // I think the dictionary is a pig on performance
    //      bool isInDictionary = false;
    //      try
    //      {
    //        isInDictionary = DictionaryManager.IsWordInDictionary(match.Value);
    //      }
    //      catch (Exception ex)
    //      {

    //      }

    //      if (!isInDictionary)
    //      {
    //        // It's not in the dicitonary so it's probably a chord
    //        chordCharacterCount += Convert.ToDecimal(match.Value.Length);
    //        chordWordCount++;
    //      }
    //    }

    //  }

    //  int nonWhiteSpaceCharacterCount = 0;

    //  foreach (Match match in Regex.Matches(sentence, @"\S"))
    //  {
    //    // Maybe consider weighting punctuation characters higher (chords don't usually have periods... could have slashes and sometimes commas
    //    nonWhiteSpaceCharacterCount += match.Value.Length;
    //  }

    //  decimal percent = 0;

    //  if (nonWhiteSpaceCharacterCount > 0)
    //  {
    //    percent = chordCharacterCount / Convert.ToDecimal(nonWhiteSpaceCharacterCount);
    //  }

    //  percent *= 100;
    //  return Convert.ToInt32(percent);

    //}

    //public static bool IsChordLine(string chords, int minimumChordMatchPercent)
    //{
    //  return (GetIsChordSentencePercent(chords) >= minimumChordMatchPercent);
    //}

    //public static string GetTranposedChordsForMultipleLines(string chords, string originalKey, string targetKey, int minimumChordLineMatchPercentage, char[] lineDelimiters)
    //{
    //  string transposedText = string.Empty;

    //  int delimiterIndex = chords.IndexOfAny(lineDelimiters, transposedText.Length);

    //  // This logic misses the last line... maybe use chords.Split(lineDelimiters).Length in a FOR loop and change logic on last loop through



    //  while (delimiterIndex > -1)
    //  {
    //    string delimiterCharacter = chords.Substring(delimiterIndex, 1);
    //    string line = chords.Substring(transposedText.Length, delimiterIndex - transposedText.Length);

    //    if (GetChordMatchPercent(line) >= minimumChordLineMatchPercentage)
    //    {
    //      line = GetTranposedChords(line, originalKey, targetKey);
    //    }

    //    transposedText += line + delimiterCharacter;

    //    delimiterIndex = chords.IndexOfAny(lineDelimiters, transposedText.Length);
    //  }

    //  return transposedText;

    //}

    //public static string[] GetChords(string[] songLines, int minimumChordMatchPercent, bool unique)
    //{

    //  List<string> chords = new List<string>();

    //  foreach (string songLine in songLines)
    //  {
    //    if (GetChordMatchPercent(songLine) >= minimumChordMatchPercent)
    //    {

    //      MatchCollection matches = Regex.Matches(songLine, ChordTransposer.ChordPattern);

    //      for (int i = matches.Count - 1; i >= 0; i--)
    //      {
    //        Match match = matches[i];

    //        if (unique == false || !chords.Contains(match.Value))
    //        {
    //          chords.Add(match.Value);
    //        }
    //      }
    //    }
    //  }

    //  return chords.ToArray();

    //}

    //public static string GetTransposedChords(string chords, string originalKey, string transposeTo)
    //{



    //  MatchCollection matches = Regex.Matches(chords, ChordTransposer.ChordPattern);

    //  for (int i = matches.Count - 1; i >= 0; i--)
    //  {

    //    Match match = matches[i];

    //    string targetChord = ChordTransposer.GetTransposedChord(match.Value, originalKey, transposeTo);

    //    chords = chords.Remove(match.Index, match.Value.Length);
    //    chords = chords.Insert(match.Index, targetChord);

    //  }


    //  return chords;

    //}

  }
}
